﻿using System.Threading.Tasks;
using System.Windows;
using client.helpers;
using client.models;

namespace client.viewmodels
{
    public class MainViewModel : ViewModelBase
    {
        private UserInfo _userInfo = null;

        public MainViewModel()
        {
        }

        #region property
        public string Version
        {
            get
            {
                var ver = Application.ResourceAssembly.GetName().Version;
                return $"v{ver.Major}.{ver.Minor}";
            }
        }

        private bool _isLogin;
        public bool IsLogin
        {
            get => _isLogin;
            set
            {
                if (_isLogin != value)
                {
                    _isLogin = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private string _currentVisiblePage = null;
        public string CurrentVisiblePage
        {
            get => _currentVisiblePage;
            set
            {
                if (_currentVisiblePage != value)
                {
                    _currentVisiblePage = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private bool _isBusy = false;
        public bool IsBusy
        {
            get => _isBusy;
            set
            {
                if (_isBusy != value)
                {
                    _isBusy = value;
                    this.OnPropertyChanged();
                }
            }
        }
        #endregion property

        public async Task Login(string user, string password)
        {
            IsBusy = true;

            var service = new ServiceApi();
            _userInfo = await service.Login(user, password);
            if (_userInfo != null)
            {
                IsLogin = true;
                CurrentVisiblePage = Constant.TestCasePageName;
            }
            else
            {
                MessageBox.Show(MainWindowReference.MainWindow,
                                LocStringHelper.GetInstance().GetString("ErrorMessageBoxMsg_LoginFail"),
                                LocStringHelper.GetInstance().GetString("ErrorMessageBoxTitle"),
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }

            IsBusy = false;
            return;
        }
    }
}
